package com.my.hotel.controller.liuhao;

import com.my.hotel.entity.KfRoom;
import com.my.hotel.entity.Order;
import com.my.hotel.entity.User;
import com.my.hotel.service.KfRoomService;
import com.my.hotel.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping(value = "/tbOrder",produces = "text/html;charset=utf-8")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private KfRoomService kfRoomService;

    @RequestMapping("/order_num")
    public String order_num(HttpServletRequest request) {
        int maxId = orderService.kf_order_maxId();
        String orderNum = String.valueOf(100 + maxId);
        request.setAttribute("orderNum", orderNum);
        //通过客房id查询客房对象
        Object kfId = request.getAttribute("kfId");
        int id = (Integer)kfId;
        KfRoom kfRoomById = kfRoomService.getKfRoomById(id);
        request.setAttribute("kfRoomById", kfRoomById);
        return "forward:/kf_order.jsp";

    }

    @RequestMapping("/kf_order")
    public String kf_order(HttpServletRequest request, Order order) {
        order.setOrderStatus("0");
        order.setOrderPayStatus("0");
        User userinfo = (User) request.getSession().getAttribute("userinfo");
        order.setUserId(userinfo.getId());
        orderService.kf_order(order);
        return "forward:/kfRoom/kfRoomyd";
    }

    @RequestMapping("/yd_rz")
    @ResponseBody
    public String yd_rz(HttpServletRequest request,String yd_value,String rz_value ) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date yd_date = format.parse(yd_value.replace("T"," "));
        Date rz_date = format.parse(rz_value.replace("T"," "));
        if (rz_date.getTime()-yd_date.getTime()<0){
            return "1";//为1表示入住时间小于预定时间，异常
        }else {
         return "0";   //为0表示入住时间调用预定时间，正常
        }

    }
}